import threading
import logging
from django.conf import settings

logger = logging.getLogger(__name__)


class Machine:
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port
        self.load = 0  # 主机负载
        self.mtx = threading.Lock()  # 互斥锁，保证线程安全

    def inc_load(self):
        """增加主机负载"""
        with self.mtx:
            self.load += 1

    def dec_load(self):
        """减少主机负载"""
        with self.mtx:
            self.load -= 1

    def reset_load(self):
        """重置主机负载"""
        with self.mtx:
            self.load = 0

    def get_load(self):
        """获取主机负载"""
        with self.mtx:
            return self.load

    def __str__(self):
        return f"Machine[{self.ip}:{self.port}, load:{self.load}]"